package xiaohu.day4.HJ45;

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            Integer n = Integer.valueOf(in.nextLine());
            for (int i = 0; i < n; i++) {
                String str = in.nextLine().toLowerCase();
                if (str.length() == 0) {
                    System.out.println(0);
                }
                int[] ints = new int[26];
                for (int j = 0; j < str.length(); j++) {
                    ints[str.charAt(j) - 'a']++;
                }
                Arrays.sort(ints);
                int count = 0;
                for (int j = 26; j >0 ; j--) {
                    if (ints[j-1] == 0) {
                        break;
                    }
                    count= count + ints[j-1]*j;
                }
                System.out.println(count);
            }

        }
    }
}
